/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkwserver.singingcert;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.DatatypeConverter;
import org.jdom.Element;
import pkwserver.PersonalData;
import pkwserver.commandline.SignRequest;
import sun.security.pkcs.PKCS10;
import sun.security.pkcs.PKCS10Attribute;
import sun.security.pkcs.PKCS10Attributes;

/**
 *
 * @author Maciek
 */
public class SignCertificate {
    
    
    static private String path(PersonalData pd){
        StringBuilder sb = new StringBuilder();
        sb.append("certs").append(File.separatorChar); 
        sb.append(pd.firstName).append(pd.lastName).append(pd.Pesel);
        return sb.toString();
    }
    
    static private String bytesToString(byte[] bytes){
        return DatatypeConverter.printBase64Binary(bytes);
    }
    
    static private byte[] stringToBytes(String string){
        return DatatypeConverter.parseBase64Binary(string);
    }
    
    
    static boolean checkData(PersonalData pd, String password, Element sigrec){
        return true;
    }
    
    static private void writeCSR(PersonalData pd, byte[] pkcs10) throws FileNotFoundException, IOException{
            FileOutputStream fileOutputStream = new FileOutputStream(path(pd) + ".csr");
            PrintStream ps = new PrintStream(fileOutputStream);
            ps.write(pkcs10);
            fileOutputStream.close();
    }
    
    
    static public int dealWithSignReq(Element sigrec, PersonalData pd, String password){
        System.out.println("Teraz spróbuje wypisać PKCS10");
         
            String helpString = sigrec.getValue();
            byte[] pkcs10  = stringToBytes(helpString);
        try {
           
        
            
            /*    try{
                PKCS10 cert = new PKCS10(pkcs10);
                PKCS10Attributes attributes = cert.getAttributes();
              
                Enumeration<PKCS10Attribute> elements = attributes.getElements();
                while(elements.hasMoreElements()){
                    PKCS10Attribute pks= elements.nextElement();
                    System.out.println(pks.getAttributeId().toString() + " " + pks.getAttributeValue().toString());
                    
                }
            }
            catch(Exception ex){
                
                System.err.println(ex.toString());
            }*/ // to niestety nie dziala
            writeCSR(pd,pkcs10);
            return  SignRequest.signRequest(pd, password);
            
        } catch (InterruptedException ex) {
            Logger.getLogger( SignCertificate.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger( SignCertificate .class.getName()).log(Level.SEVERE, null, ex);
        } 
         return -1;
    }
}
